clear all; clc; close all

rng('shuffle') % перезапуск случ.чисел

N = 100000;

% % uniform
% % Сформировать 100 000 реализаций СВ, равномерно распределенной от 10 до 22. Построить гистограмму. 
% 
a = 10; b = 22;
% x = a + (b-a)*rand(1,N);
% 
% bins = (a-2):0.1:(b+2);
% [yh, xh] = hist(x, bins);
% 
% figure;
% 
% hist(x, bins);
% hold on
% plot(xh, yh, 'r', 'LineWidth', 3)
% hold off
% 
% figure;
% dxh = xh(2) - xh(1);
% ph = yh / (sum(yh)*dxh);
% 
% % [nul, i1, i2] = intersect(bins, xh);
% % tmp = zeros(1, length(bins));
% % tmp(i1) = ph; ph = tmp;
% % xh = bins;
% 
% plot(xh, ph);
% xlabel('X'); ylabel('P(X)');
% ylim([0 1.1*max(ph)]);
% xlim([a-1 b+1]);

% % gaus
% 
% sko = 2; m = 16;
% x = m + sko*randn(1,N);
% 
% bins = (m-4*sko):(sko/4):(m+4*sko);
% [yh, xh] = hist(x, bins);
% 
% figure;
% 
% hist(x, bins);
% hold on
% plot(xh, yh, 'r', 'LineWidth', 3)
% hold off
% 
% figure;
% dxh = xh(2) - xh(1);
% ph = yh / (sum(yh)*dxh);
% 
% plot(xh, ph);
% xlabel('X'); ylabel('P(X)');
% ylim([0 1.1*max(ph)]);
% xlim([a-1 b+1]);

% % chi 2
% 
% sko = 2; m = 4;
% x1 = m + sko*randn(1,N);
% x2 = m + sko*randn(1,N);
% 
% x = x1.^2 + x2.^2;
% 
% [nul, bins] = hist(x, 20);
% [yh, xh] = hist(x, bins);
% 
% figure;
% 
% hist(x, bins);
% hold on
% plot(xh, yh, 'r', 'LineWidth', 3)
% hold off
% 
% figure;
% dxh = xh(2) - xh(1);
% ph = yh / (sum(yh)*dxh);
% 
% plot(xh, ph);
% xlabel('X'); ylabel('P(X)');
% ylim([0 1.1*max(ph)]);
% xlim([0 250]);

% % reley 
% 
% sko = 2; m = 0;
% x1 = m + sko*randn(1,N);
% x2 = m + sko*randn(1,N);
% 
% x = sqrt(x1.^2 + x2.^2);
% 
% [nul, bins] = hist(x, 20);
% [yh, xh] = hist(x, bins);
% 
% figure;
% 
% hist(x, bins);
% hold on
% plot(xh, yh, 'r', 'LineWidth', 3)
% hold off
% 
% figure;
% dxh = xh(2) - xh(1);
% ph = yh / (sum(yh)*dxh);
% 
% plot(xh, ph);
% xlabel('X'); ylabel('P(X)');
% ylim([0 1.1*max(ph)]);

% % rice 
% 
% sko = 2; m = 10;
% x1 = m + sko*randn(1,N);
% x2 = m + sko*randn(1,N);
% 
% x = sqrt(x1.^2 + x2.^2);
% 
% [nul, bins] = hist(x, 20);
% [yh, xh] = hist(x, bins);
% 
% figure;
% 
% hist(x, bins);
% hold on
% plot(xh, yh, 'r', 'LineWidth', 3)
% hold off
% 
% figure;
% dxh = xh(2) - xh(1);
% ph = yh / (sum(yh)*dxh);
% 
% plot(xh, ph);
% xlabel('X'); ylabel('P(X)');
% ylim([0 1.1*max(ph)]);

% chol

L = chol([16  12  12;
          12  16  8;
          12  8   16 ])';

x = nan(1,N);
y = nan(1,N);
z = nan(1,N);

for i=1:N

    n = L*randn(3,1);
    
    x(i) = n(1);
    y(i) = n(2);
    z(i) = n(3);
    
end

figure;

plot3(x,y,z)
% hist(x, bins);
% hold on
% plot(xh, yh, 'r', 'LineWidth', 3)
% hold off
% 
% figure;
% dxh = xh(2) - xh(1);
% ph = yh / (sum(yh)*dxh);
% 
% plot(xh, ph);
% xlabel('X'); ylabel('P(X)');
% ylim([0 1.1*max(ph)]);
